<?php
class member extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "用户";
		$this->modelObj = D('company_admin');
		$this->roleObj = D('company_role');

		$this->cityObj = D('sys_city');
		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');

		$this->deviceObj = D('company_device');
		$this->diaochaObj = D('company_diaocha');

		$this->userObj = D('company_user');

		$this->score_logObj = D('company_score_log');
		$this->money_logObj = D('company_money_log');

		// $this->data_list = $this->roleObj->getTreeList("status=1".$this->cond_area);

		$areas = $this->areaObj->findAll(array("status=1"));
		if($areas){
			foreach($areas as $val){
				$areas_arr[] = $val['id'];
			}
			$this->area_ids = implode(',',$areas_arr);
		}

		$cates = $this->cateObj->findAll(array("status=1"));
		if($cates){
			foreach($cates as $val){
				$cates_arr[] = $val['id'];
			}
			$this->cate_ids = implode(',',$cates_arr);
		}

		$zones = $this->zoneObj->findAll(array("status=1"));
		if($zones){
			foreach($zones as $val){
				$zones_arr[] = $val['id'];
			}
			$this->zone_ids = implode(',',$zones_arr);
		}


	}

	//列表
	public function index(){
		$this->tabtit = array_merge(array('全部'),$this->userLevels);

		$cond = "is_admin=0 and level>0 ".$this->cond_base;

		//加入查询条件
		if($this->tab){
			$cond .= " and status=1 and level={$this->tab}";
		}else{
			$cond .= " and status=1";
		}
		//搜索
		if($this->keyword){
			$cond.=" and (name like '%".$this->keyword."%') ";
		}

		$result = $this->modelObj->get_pagelist($cond);
        if($result){
			$this->users = $this->userObj->ids($result['list'],'user_id');

        	$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}

	}

	//添加
	public function add(){

		$this->tab = arg('tab') ? arg('tab') : 1 ;

		if($this->isAjax()){
			$data = $this->postData('data');
			if($data['username']){
				$username = $data['username'];
				$res = $this->modelObj->check_user($username);
				if($res){
					$this->renderError('用户名已存在');
				}
			}else{
				$data['username'] = $data['tel'];
			}

			$data['company_id'] = $this->company['id'];
			$data['cate_ids'] = $this->cate_ids;
			$data['zone_ids'] = $this->zone_ids;
			$data['area_ids'] = $this->area_ids;


			$this->modelObj->add_user($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('status'=>1,'sex'=>1,'level'=>$this->tab,'is_admin2'=>0);
		// $this->accessList = $this->accessObj->getJsTree("type=2");
		// dump($this->accessList);exit;
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			// if($data['password']){
			// 	$data['password'] = qcing_hash($data['password']);
			// }else{
			// 	unset($data['password']);
			// }
			if(!$data['username']){
				$data['username'] = $data['tel'];
			}
			$this->modelObj->edit_user($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$info = $this->modelObj->findById($id);
		$this->info = $info;
		// $cond = "status=1".$this->cond_base;
		// $this->areaList = $this->areaObj->getJsTree($cond,$this->info['area_ids']);
		// $this->cateList = $this->cateObj->getJsTree($cond,$this->info['cate_ids']);
		// $this->zoneList = $this->zoneObj->getJsTree($cond,$this->info['zone_ids']);
		// dump($this->cateList);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //隐藏
	public function ban(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->act($id,0);
			$this->renderSuccess('隐藏成功');
		}
	}

	//显示
	public function act(){
		$id = arg('id');
		$status = arg('status');
		if($status==1){
			$msg = '审核';
			//权限分配
			$admin = $this->modelObj->findById($id);
			// $this->company = $this->companyObj->findById($admin['company_id']);

			// $arrBase['area_ids'] = $this->company['city_ids'];
			// $arrBase['cate_ids'] = $this->company['cate_ids'];
			// $arrBase['store_ids'] = $admin['id'];
			$this->modelObj->act($id,$status);
		}else{
			$msg = '隐藏';
		}
		if($this->isAjax()){
			$res = $this->modelObj->act($id,$status);
			$this->renderSuccess($msg.'成功');
		}
	}

	//上班
	public function sign(){
		$id = arg('id');
		$status = arg('status');
		if($status==1){
			$msg = '上班';
			$admin = $this->modelObj->findById($id);
			$this->modelObj->act($id,$status,'is_sign');
		}else{
			$msg = '下班';
		}
		if($this->isAjax()){
			$res = $this->modelObj->act($id,$status,'is_sign');
			$this->renderSuccess($msg.'成功');
		}
	}

	//值班
	public function zhiban(){
		$id = arg('id');
		$status = arg('status');
		if($status==1){
			$msg = '值班';
			$admin = $this->modelObj->findById($id);
			$this->modelObj->act($id,$status,'is_zhiban');
		}else{
			$msg = '取消';
		}
		if($this->isAjax()){
			$res = $this->modelObj->act($id,$status,'is_zhiban');
			$this->renderSuccess($msg.'成功');
		}
	}

    //删除
	public function del(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->del($id);
			$this->renderSuccess('删除成功');
		}
	}

	//排序
	public function sort() {
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
    }

	//管理
	public function login(){
		$id = arg('id');
		$cond = "id='{$id}' and status=1 ".$this->cond_base;
		$admin = $this->modelObj->find(array($cond));
		// dump($user);exit;
		$_SESSION['company_user']['id'] = $admin['id'];
        $_SESSION['logintime'] = time();
        // set_cookie('company_admin_mnm',$user['username'],time()+3600*24);

		$this->redirect('/index_admin/my/index');
	}

	// 修改密码
	public function pwd(){
		if($this->isAjax()){

			$data = $this->postData('data');
			$data['username'] = $this->admin['username'];
			if ($data['password'] !== $data['password_confirm']) {
				$this->renderError('确认密码不正确');
			}
	        $admin = $this->modelObj->pwd_user($data);
	        if($admin){
				// $_SESSION["admin"] = $admin;
	            $this->renderSuccess('更新成功', '/admin/login/logout');
	        }else{
	            $this->renderError('更新失败');
	        }

		}
	}

	//编辑
	public function auth(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['area_ids'] = joinStr($data['area']);
			$arrBase['cate_ids'] = joinStr($data['cate']);
			$arrBase['zone_ids'] = joinStr($data['zone']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$cond_area = "status=1 ".$this->cond_base;
		$cond_cate = "status=1 ".$this->cond_base;
		$cond_zone = "status=1 ".$this->cond_base;
		$this->areaList = $this->areaObj->get_JsTree($cond_area,$this->info['area_ids']);
		$this->cateList = $this->cateObj->get_JsTree($cond_cate,$this->info['cate_ids']);
		$this->zoneList = $this->zoneObj->get_JsTree($cond_zone,$this->info['zone_ids']);

	}


	//充值
	public function recharge()
	{

		$id = arg('id');
		if($this->isAjax()){

			// recharge[balance][mode]

			$recharge = $this->postData('recharge');
			$source = arg('source');
			$user_id = arg('user_id');

			$balance = $recharge['balance'];
			if($balance['value']>0){
				$remark = $balance['remark'] ?? '管理员后台操作';

				switch($balance['mode']){
					case 'inc':
						$arrBase = array(
							'company_id' => $this->company['id'],
							'admin_id' => $user_id,
							'type' => 0,
							'money' => $balance['value'],
							'msg' => $remark,
						);
						$this->money_logObj->add_log($arrBase);

						$this->modelObj->add_money($user_id,$balance['value']);
						break;
					case 'dec':
						$arrBase = array(
							'company_id' => $this->company['id'],
							'admin_id' => $user_id,
							'type' => 0,
							'money' => -$balance['value'],
							'msg' => $remark,
						);
						$this->money_logObj->add_log($arrBase);

						$this->modelObj->add_money($user_id,-$balance['value']);
						break;
					case 'final':

						$arrBase = array(
							'company_id' => $this->company['id'],
							'admin_id' => $user_id,
							'type' => 0,
							'money' => $balance['value'],
							'msg' => $remark,
						);
						$this->money_logObj->add_log($arrBase);

						$arrBase = array(
							'money' => $balance['value'],
						);
						$this->modelObj->edit($user_id,$arrBase);
						//增加积分
						$this->modelObj->add_money($user_id,$balance['value']);
						break;
				}
			}

			// $points = $recharge['points'];
			// if($points['value']>0){
			// 	// $mode = $points['mode'];
			// 	// $value = $points['value'];
			// 	$remark = $points['remark'] ?? '管理员后台操作';

			// 	switch($points['mode']){
			// 		case 'inc':
			// 			// $this->modelObj->incr(array('id'=>$user_id),'score',$score);
			// 			//增加积分
			// 			$this->modelObj->add_score($user_id,$points['value'],$remark);
			// 			break;
			// 		case 'dec':
			// 			// $this->modelObj->decr(array('id'=>$user_id),'score',$score);
			// 			//减少积分
			// 			$this->modelObj->add_score($user_id,-$points['value'],$remark);
			// 			break;
			// 		case 'final':
			// 			$arrBase = array(
			// 				'score' => $points['value'],
			// 			);
			// 			$this->modelObj->edit($user_id,$arrBase);
			// 			//增加积分
			// 			$this->modelObj->add_score($user_id,0,'后台积分重置');
			// 			break;
			// 	}
			// }

			$this->renderSuccess('充值成功');
		}
	}

	//科室权限
	public function auth_zone(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['zone_ids'] = joinStr($data['zone']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$cond = "status=1 ".$this->cond_base;
		$this->dataList = $this->zoneObj->get_JsTree($cond,$this->info['zone_ids']);


		$this->layout = "admin/layout2.php";
	}

	//调查权限
	public function auth_diaocha(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['diaocha_ids'] = joinStr($data['diaocha']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$js_data = [
			'id' => '-100',
			'parent_id' => 0,
			'title' => '具体意见或建议',
		];

		$cond_diaocha = "status=1".$this->cond_base;
		$this->diaochaList = $this->diaochaObj->get_JsTree($cond_diaocha,$this->info['diaocha_ids'],'title',$js_data);


		$this->layout = "admin/layout2.php";
	}


	//调查权限
	public function weixin(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['user_id'] = $data['user_id'];

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$this->layout = "admin/layout2.php";
	}

}